// @Author EthanScriptOn
// @Desc
package mysql_operator

import (
	"fmt"
	"gitee.com/fatzeng/srf_switch_basic_components/common/identity"
	"gitee.com/fatzeng/srf_switch_expansion_package/connect"
	"gitee.com/fatzeng/srf_switch_stream_bridge/framework/actuator"
)

type MysqlExecOperator struct {
	*identity.IdentityInformationCarrier
}

func (m *MysqlExecOperator) Execute(ctx *actuator.SrfSwitchBridgeActuatorContext, configIdentifier interface{}, structureParam interface{}) (interface{}, error) {
	commandSql, ok := structureParam.(string)
	if !ok {
		return nil, fmt.Errorf("structureParam is not string")
	}
	if commandSql == "" {
		return nil, fmt.Errorf("commandSql is empty")
	}
	db, err := connect.GetMysqlExecDB()
	if err != nil {
		return nil, fmt.Errorf("unable to obtain MySQL connection")
	}
	defer db.Close()
	_ = ctx.Record(actuator.OperatorExecute, fmt.Sprintf("Execute SQL: %s", commandSql))
	affected, err := db.Exec(commandSql)
	if err != nil {
		return nil, fmt.Errorf("failed to execute SQL exec: %v", err)
	}
	return affected, nil
}
